package integral

import (
	"context"

	"mall/service/backend/api/internal/svc"
	"mall/service/backend/api/internal/types"
	"mall/service/user/rpc/userclient"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetIntegralTaskLogListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 获取积分任务日志列表
func NewGetIntegralTaskLogListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetIntegralTaskLogListLogic {
	return &GetIntegralTaskLogListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetIntegralTaskLogListLogic) GetIntegralTaskLogList(req *types.IntegralTaskLogListReq) (resp *types.BackendUserTaskLogListResp, err error) {
	logx.Infof("GetIntegralTaskLogList req:%+v", req)
	res, err := l.svcCtx.UserRpc.BackendGetIntegralTaskLogList(l.ctx, &userclient.IntegralTaskLogListReq{
		Ids:       req.Ids,
		UserId:    req.UserId,
		WithCycle: req.WithCycle,
		StartTime: req.StartTime,
		EndTime:   req.EndTime,
	})
	if err != nil {
		return nil, err
	}
	var list []*types.IntegralTaskLogInfo
	var points uint64
	if len(res.List) > 0 {
		for _, v := range res.List {
			list = append(list, &types.IntegralTaskLogInfo{
				Id:        v.Id,
				UserId:    v.UserId,
				TaskId:    v.TaskId,
				TaskName:  v.TaskName,
				TaskSlug:  v.TaskSlug,
				Gems:      v.Gems,
				Growth:    v.Growth,
				Integrals: v.Integrals,
				Status:    v.Status,
				CreatedAt: v.CreatedAt,
				UpdatedAt: v.UpdatedAt,
			})
			if v.Integrals != nil && *v.Integrals > 0 {
				points += *v.Integrals
			}
		}
	}
	return &types.BackendUserTaskLogListResp{
		Total:  res.Total,
		List:   list,
		Points: points,
	}, nil
}
